/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <string>
#include <vector>
#include <memory>
#include <baja/db/db.h>
#include <journal/models/db/table_interfaces.h>

namespace journal { namespace model { namespace db
{

class media_file_table :
    public imedia_file_table,
    public icommon_table_client
{
public:
    media_file_table();
    static std::shared_ptr<imedia_file_table> create(std::shared_ptr<s::iengine> dbEngine);

    // icommon_table_client
    void defineColumns(std::shared_ptr<s::itable> table);
    void defineIndexColumns(std::shared_ptr<s::iindex> primaryKeyIndex);
    void defineRowCells(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> row);

    // ibase_table
    std::shared_ptr<icommon_table> common();

    // imedia_file_table
    void insert(b::uint32 mediaFileId,
                std::wstring fileName,
                std::wstring originalFileName);

    void load(
        b::uint32 mediaFileId,
        std::wstring& fileName,
        std::wstring& originalFileName
        );

    bool findMediaFile(
        std::wstring originalFileName,
        b::uint32& mediaFileId,
        std::wstring& localMediaFileName
        );

    void disposeUnused(
        const std::set<b::uint32>& inUseSet,
        std::vector<std::wstring>& filesToDispose);

private:
    std::recursive_mutex _mutex;

    std::shared_ptr<icommon_table> _commonTable;
    std::weak_ptr<icommon_table_client> _commonTableClient;

    std::shared_ptr<s::icolumn_typed<b::uint32>> _mediaFileIdColumn;
    std::shared_ptr<s::icolumn_typed<std::wstring>> _fileNameColumn;
    std::shared_ptr<s::icolumn_typed<std::wstring>> _originalFileNameColumn;

    std::shared_ptr<s::icell_typed<b::uint32>> _mediaFileIdCell;
    std::shared_ptr<s::icell_typed<std::wstring>> _fileNameCell;
    std::shared_ptr<s::icell_typed<std::wstring>> _originalFileNameCell;

    // PK index.
    std::shared_ptr<s::iindex_column> _mediaFileIdIndexColumn;

    // OriginalFileName index.
    std::shared_ptr<s::iindex> _originalFileNameIndex;
    std::shared_ptr<s::iindex_column> _originalFileNameIndexColumn;

    void read(
        b::uint32 mediaFileId,
        std::shared_ptr<s::iresult> result
        );

    void readOriginalFileName(
        std::wstring originalFileName,
        std::shared_ptr<s::iresult> result
        );

    void readIds(
        std::set<b::uint32>& mediaFileIds);

    void findIdComplementSet(
        const std::set<b::uint32>& subSet,
        std::set<b::uint32>& subSetComplement);

    void dispose(
        const b::uint32& mediaFileId);


};

std::shared_ptr<imedia_file_table> createMediaFileTable(std::shared_ptr<s::iengine> dbEngine);

}}} // journal::model::db